﻿#ifndef PYTHONAGENT_H
#define PYTHONAGENT_H

#include <QObject>
#include <QStringList>
#include "API_ScriptManager.h"

class QThread;

namespace ScriptManager
{
    class PyInterpreter;
    class ScriptThread;
    class RecordThread;

    class API_SCRIPTMANAGER PythonAgent : public QObject
    {
        Q_OBJECT
    public:
        static PythonAgent* getInstance();
        void initialize();
        void finalize(); 

        /// @brief 执行python代码
        /// @param code python代码 
        /// @param save 是否保存
        /// @param exec 是否执行
        void submit(QString code, bool save = true, bool exec = true);
        void submit(QStringList codes, bool save = true, bool exec = true);

        /// @brief 从执行脚本文件线程提交
        void submitFromScriptThread(QString code, bool save = true, bool exec = true);

        /// @brief 从执行脚本文件线程提交
        void submitFromScriptThread(QStringList codes, bool save = true, bool exec = true);

        /// @brief 将命令添加到脚本线程中执行
        /// @param command 命令
        /// @param filePath 文件路径
        /// @return 是否添加成功
        bool appendCommandToPyThread(QString filePath,QStringList command);

        //后台执行，不在界面显示，也不保存
        void backstageExec(QString code);
        void saveScript(QString fileName);
        bool execScriptFile(QString fileName, bool isNoGui, QStringList listPara = QStringList());
        bool execScriptCode(QStringList listCode, bool isNoGUI, QStringList listPara = QStringList());
        void appCodeList(QString code);
        void lock();
        void unLock();
        bool isLocked();
        void appendOn();
        void appendOff();
        void execMessWinCode(QString code);
        QStringList getcodelist();
        void setIsGUI(bool isGui);

        /// @brief 执行失败
        void stopScript();

        /// @brief 设置python调试
        /// @param isDebug 是否是调试
        void setDebug(bool isDebug);

        /// @brief 脚本线程是否正在执行
        /// @return 是否正在执行
        bool isScriptRuning();

        /// @brief 设置导入案例
        void setIsImportExample();

        /// @brief 重新开始记录脚本
        void restartRecordScript();

    private:
        PythonAgent();
        ~PythonAgent() = default;

        /// @brief 执行python代码
        /// @param code python代码 
        /// @param save 是否保存
        /// @param exec 是否执行
        void execCode(QString code, bool save = true, bool exec = true);

        /// @brief 执行python代码
        /// @param code python代码 
        /// @param save 是否保存
        /// @param exec 是否执行
        void execCode(QStringList codes, bool save = true, bool exec = true);

    private slots:
        void readerFinished();

    private:
        static PythonAgent* m_instance;
        PyInterpreter* m_interpreter{};
        RecordThread* m_recordScript{};
        ScriptThread* m_scriptThread{};
        bool m_islock{ false };
        bool m_append{ true };

        bool m_isGUI{ false };
        QThread* m_ptrThread{};
        /// @brief 是否是调试模式，调试模式执行失败也会往下执行
        bool m_isDebug{ false };
    };
}

#endif // PYTHONAGENT_H
